package neusoft.edu.cn.guanshangshiguang.activities;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import java.util.List;

import neusoft.edu.cn.guanshangshiguang.Listener.ListListener;
import neusoft.edu.cn.guanshangshiguang.Listener.OnItemClickListener;
import neusoft.edu.cn.guanshangshiguang.R;
import neusoft.edu.cn.guanshangshiguang.adapter.OnlineBookListAdapter;
import neusoft.edu.cn.guanshangshiguang.bean.OnlineBookListBean;
import neusoft.edu.cn.guanshangshiguang.model.OnlineBookListModel;

public class OnlineBookList_Activity extends AppCompatActivity {

    private List<OnlineBookListBean> List;
    RecyclerView recyclerView;
    OnlineBookListAdapter viewAdapter;
    private String tid;


    ListListener listListener = new ListListener() {
        @Override
        public void onResponse(List list) {
            List = list;
            viewAdapter.setDate(List);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(), "失败："+msg, Toast.LENGTH_SHORT).show();
        }
    };


    OnItemClickListener onItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Intent intent = new Intent(getApplicationContext(),ArticleDetailsActivity.class);
            intent.putExtra("bid",List.get(position).getBid());
            intent.putExtra("bpic",List.get(position).getBpic());
            intent.putExtra("bname",List.get(position).getBname());
            intent.putExtra("intro",List.get(position).getIntroduction());
            intent.putExtra("content",List.get(position).getContent());
            startActivity(intent);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();//去掉标题栏
        actionBar.hide();//去掉标题栏
        setContentView(R.layout.activity_book_details_);

        RecyclerViewInit();

        tid=getIntent().getStringExtra("tid");
        OnlineBookListModel onlineBookListModel = new OnlineBookListModel();
        onlineBookListModel.getResultList(tid,listListener);

    }

    private void RecyclerViewInit() {
        viewAdapter = new OnlineBookListAdapter();

        recyclerView = findViewById(R.id.recyclerView);

        //布局管理器  可以设置显示数据的方式为垂直或者水平
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);

//        //添加自己设计的分割线
//        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL);
//        dividerItemDecoration.setDrawable(ContextCompat.getDrawable(getContext(),R.drawable.divider));
//        recyclerView.addItemDecoration(dividerItemDecoration);

        recyclerView.setLayoutManager(linearLayoutManager);
        viewAdapter.setOnItemClickListener(onItemClickListener);
        recyclerView.setAdapter(viewAdapter);
    }
}
